﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DataLibrary
{
   public class Ctbl_News :IData<tbl_News>
    {
       public Ctbl_News(String myconnecString) : base(myconnecString) { }


       

       public override List<tbl_News> GetAll(tbl_News item)
       {
           throw new NotImplementedException();
       }

       public override tbl_News GetItemById(object id)
       {
           return GetAll().SingleOrDefault(p => p.IdNew == Convert.ToInt32(id));
       }

       public override tbl_News GetItem(IDataReader dr)
       {
           tbl_News obj = new tbl_News();
           obj.AddedDate = Convert.ToDateTime(dr[tbl_News.c_addedDate]);
           obj.Description = Convert.ToString(dr[tbl_News.c_Description]);
           obj.IdCat = Convert.ToInt32(dr[tbl_News.c_idCat]);
           obj.IdNew = Convert.ToInt32(dr[tbl_News.c_idNew]);
           obj.ImageNew = Convert.ToString(dr[tbl_News.c_imageNew]);
           obj.IsActive = Convert.ToInt32(dr[tbl_News.c_isActive]);
           obj.IsAuto = Convert.ToBoolean(dr[tbl_News.c_isAuto]);
           obj.IsTindacbiet = Convert.ToBoolean(dr[tbl_News.c_isTindacbiet]);
           obj.OrderId = Convert.ToInt32(dr[tbl_News.c_orderId]);
           obj.IdTrangThai = Convert.ToInt32(dr[tbl_News.c_IdTrangThai]);
           return obj;
       }

       public override List<tbl_News> GetAll()
       {
           String strSQL = String.Format("select * from {0} order by idNew desc", tbl_News.b_tbl_News);
           return GetAll(strSQL, CommandType.Text);
       }


       protected override int InsertOnSubmit(tbl_News item, int t)
       {
           int id = -1;
           id = connec.ExecuteNonQuery("protbl_News",CommandType.StoredProcedure,new SqlParameter("@flag",t),
               new SqlParameter("@"+tbl_News.c_addedDate,item.AddedDate),new SqlParameter("@"+tbl_News.c_Description,item.Description),
               new SqlParameter("@"+tbl_News.c_idCat,item.IdCat),new SqlParameter("@"+tbl_News.c_idNew,item.IdNew),
               new SqlParameter("@"+tbl_News.c_imageNew,item.ImageNew),new SqlParameter("@"+tbl_News.c_isActive,item.IsActive),
               new SqlParameter("@"+tbl_News.c_isAuto,item.IsAuto),new SqlParameter("@"+tbl_News.c_isTindacbiet,item.IsTindacbiet),
               new SqlParameter("@"+tbl_News.c_orderId,item.OrderId),new SqlParameter("@"+tbl_News.c_IdTrangThai,item.IdTrangThai));
           return id;
       }
    }
}
